/*
 * Digital Audio Mobile - A multi track studio for Windows Mobile
 * Copyright (C) 2008 David Olsson
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <windows.h>

#include "AudioBuffer.h"

AudioBuffer::AudioBuffer(unsigned int numSamples, unsigned int numChannels, unsigned int samplesPerSecond, unsigned int bytesPerSample) throw(): m_numSamples(numSamples), m_numChannels(numChannels), m_samplesPerSecond(samplesPerSecond), m_bytesPerSample(bytesPerSample), m_buffer(0) {
    m_buffer = new unsigned char[m_numSamples * m_numChannels * m_bytesPerSample];
}

AudioBuffer::~AudioBuffer() {
    if(m_buffer != 0) {
        delete[](m_buffer);
    }
}

unsigned int AudioBuffer::numSamples() const throw() {
    return(m_numSamples);
}

unsigned int AudioBuffer::numChannels() const throw() {
    return(m_numChannels);
}

unsigned int AudioBuffer::samplesPerSecond() const throw() {
    return(m_samplesPerSecond);
}

unsigned int AudioBuffer::bytesPerSample() const throw() {
    return(m_bytesPerSample);
}

const void *AudioBuffer::buffer() const throw() {
    return(m_buffer);
}

void *AudioBuffer::buffer() throw() {
    return(m_buffer);
}

unsigned int AudioBuffer::bufferSize() const throw() {
    return(m_numSamples * m_numChannels * m_bytesPerSample);
}

void AudioBuffer::clear() throw() {
    if(m_bytesPerSample == 1) {
        FillMemory(m_buffer, bufferSize(), 128);
    }
    else {
        ZeroMemory(m_buffer, bufferSize());
    }
}

void AudioBuffer::adjustVolume(unsigned short volume) throw() {
    switch(m_bytesPerSample) {
        case 1:
            adjustVolumeXX8(volume);
            break;
        case 2:
            adjustVolumeXX16(volume);
            break;
    }
}

void AudioBuffer::superPosition(const AudioBuffer &rhs) throw() {
    if(m_numChannels == rhs.m_numChannels && m_samplesPerSecond == rhs.m_samplesPerSecond && m_bytesPerSample == rhs.m_bytesPerSample) {
        switch(m_numChannels) {
            case 1:
                switch(m_bytesPerSample) {
                    case 1:
                        switch(m_samplesPerSecond) {
                            case 11025:
                                superPositionXM8OnXM8(rhs);
                                break;
                            case 22050:
                                superPositionXM8OnXM8(rhs);
                                break;
                            case 41000:
                                superPositionXM8OnXM8(rhs);
                                break;
                            default:
                                ASSERT(!"Unsupported audio format!");
                                break;
                        }
                        break;
                    case 2:
                        switch(m_samplesPerSecond) {
                            case 11025:
                                superPositionXM16OnXM16(rhs);
                                break;
                            case 22050:
                                superPositionXM16OnXM16(rhs);
                                break;
                            case 41000:
                                superPositionXM16OnXM16(rhs);
                                break;
                            default:
                                ASSERT(!"Unsupported audio format!");
                                break;
                        }
                        break;
                    default:
                        ASSERT(!"Unsupported audio format!");
                        break;
                }
                break;
            case 2:
                break;
            default:
                ASSERT(!"Unsupported audio format!");
                break;
        }
    }
}

void AudioBuffer::adjustVolumeXX8(unsigned short volume) throw() {
    ASSERT(m_bytesPerSample == 1);

    for(unsigned int i = 0; i < (m_numSamples * m_numChannels); i++) {
        m_buffer[i] = static_cast<unsigned char>((((short(m_buffer[i]) - 128) * volume) / 255) + 128);
    }
}

void AudioBuffer::adjustVolumeXX16(unsigned short volume) throw() {
    ASSERT(m_bytesPerSample == 2);

    for(unsigned int i = 0; i < (m_numSamples * m_numChannels); i++) {
        reinterpret_cast<short *>(m_buffer)[i] = (reinterpret_cast<short *>(m_buffer)[i] * volume) / 255;
    }
}

void AudioBuffer::superPositionXM8OnXM8(const AudioBuffer &rhs) throw() {
    for(unsigned int s = 0; s < m_numSamples && s < rhs.m_numSamples; s++) {
        short sample = (short(m_buffer[s]) - 128) + (short(rhs.m_buffer[s]) - 128);
        if(sample > 127) {
            sample = 127;
        }
        else if(sample < -128) {
            sample = -128;
        }
        m_buffer[s] = static_cast<unsigned char>(sample + 128);
    }
}

void AudioBuffer::superPositionXM16OnXM16(const AudioBuffer &rhs) throw() {
    short *buffer = reinterpret_cast<short *>(m_buffer);
    short *rhsbuffer = reinterpret_cast<short *>(rhs.m_buffer);
    for(unsigned int s = 0; s < m_numSamples && s < rhs.m_numSamples; s++) {
        int sample = int(buffer[s]) + int(rhsbuffer[s]);
        if(sample > 32767) {
            sample = 32767;
        }
        else if(sample < -32768) {
            sample = -32768;
        }
        buffer[s] = sample;
    }
}
